package quq.missq.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.update.UmengUpdateAgent;

import quq.missq.BaseActivity;
import quq.missq.Constants;
import quq.missq.MissQApplication;
import quq.missq.R;
import quq.missq.beans.BootImageBean;
import quq.missq.utils.ImageLoadUtil;
import quq.missq.utils.VolleyTool;

/**
 * Created by ${dabing} on 2015/10/8.
 */
public class WelcomeActivity extends BaseActivity implements Animation.AnimationListener, VolleyTool.HTTPListener {
    private RelativeLayout rl_welcome_bg;
    private int Boot_IMAGE = 27;
    private BootImageBean bean;
    private ImageView netImage;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        UmengUpdateAgent.setUpdateAutoPopup(true);
        UmengUpdateAgent.update(this);
        setContentView(R.layout.welcome);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //Log.e("WelcomeActivity", "========================================================");
        initView();
        initData();
    }

    private void initData() {
        System.out.println(Constants.Boot_Image_URL);
        VolleyTool.get1(this, Constants.Boot_Image_URL, null, this, Boot_IMAGE, BootImageBean.class);
    }

    private void initView() {
        rl_welcome_bg = (RelativeLayout) findViewById(R.id.rl_welcome_bg);
        netImage = (ImageView) findViewById(R.id.net_image);
        /*// 渐变，从完全不透明到完全透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setDuration(2000);
        alphaAnimation.setAnimationListener(this);
        //动画集合,应该还有一个从小放大的动画
        rl_welcome_bg.startAnimation(alphaAnimation);*/
    }


    @Override
    public void onAnimationStart(Animation animation) {


//        netImage.setImageUrl(url, VolleyTool.getImageLoader());
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        if (bean == null) {
            //Log.e("WelcomeActivity", "------------------------------------");
        }
        //Log.e("WelcomeActivity",bean.bootImages.boot_640x960);
        findViewById(R.id.image).setVisibility(View.GONE);
        if(bean != null){
            if (bean.bootImages.boot_1242x2208 != null) {
                url = bean.bootImages.boot_1242x2208;
            } else if (bean.bootImages.boot_640x1136 != null) {
                url = bean.bootImages.boot_640x1136;
            } else if (bean.bootImages.boot_640x960 != null) {
                url = bean.bootImages.boot_640x960;
            } else if (bean.bootImages.boot_750x1334 != null) {
                url = bean.bootImages.boot_750x1334;
            }
        }

        ImageLoader.getInstance().displayImage(url, netImage, MissQApplication.pager_options);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setDuration(2000);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
//        //动画集合,应该还有一个从小放大的动画
        rl_welcome_bg.startAnimation(alphaAnimation);


    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        bean = (BootImageBean) t;
        // 渐变，从完全不透明到完全透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setDuration(2000);
        alphaAnimation.setAnimationListener(this);
        //动画集合,应该还有一个从小放大的动画
        rl_welcome_bg.startAnimation(alphaAnimation);
//        Log.e("WelcomeActivity", bean.bootImages.boot_1242x2208);
//        Log.e("WelcomeActivity",bean.bootImages.boot_640x1136);
//        Log.e("WelcomeActivity",bean.bootImages.boot_750x1334);
//        //Log.e("WelcomeActivity",bean.bootImages.boot_640x960);

    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setDuration(2000);
        alphaAnimation.setAnimationListener(this);
        //动画集合,应该还有一个从小放大的动画
        rl_welcome_bg.startAnimation(alphaAnimation);
    }
}
